package com.sqtracker.recommender.controller;

import com.sqtracker.recommender.model.TorrentScore;
import com.sqtracker.recommender.service.TorrentScoreService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/v1/torrent-scores")
public class TorrentScoreController {

    private final TorrentScoreService torrentScoreService;

    @Autowired
    public TorrentScoreController(TorrentScoreService torrentScoreService) {
        this.torrentScoreService = torrentScoreService;
    }

    @GetMapping("/{torrentId}")
    public ResponseEntity<TorrentScore> getScore(@PathVariable String torrentId) {
        return torrentScoreService.getScore(torrentId)
                .map(ResponseEntity::ok)
                .orElse(ResponseEntity.notFound().build());
    }

    @GetMapping("/top")
    public ResponseEntity<List<TorrentScore>> getTopScores() {
        return ResponseEntity.ok(torrentScoreService.getTopScores());
    }

    @PostMapping("/{torrentId}/refresh")
    public ResponseEntity<Void> refreshScore(@PathVariable String torrentId) {
        torrentScoreService.calculateAndUpdateScore(torrentId);
        return ResponseEntity.ok().build();
    }
} 